
(function($, win, doc){
	
	/**
	 *  默认值：/seeyon
	 */
	var appBasePath = (function(){
		
		var path = location.pathname;
		
		return path.substring(0, path.indexOf('/', 1));
	})();
	
	
	var app = new Vue({
		el: '#app',
		data: {
			basic:{
				
			},
			style:{
				
			},
			formData:{
				fields:{
					id: null,
					name: '',
					workflowId: null,
					templateId: null,
					formId: null,
					description: null
				},
				options: {
					workflow:[
						{name: 'A8', mark: '---'}
					]
				},
				rules: {
				  name: [
					  { required: true, message: '请输入名称', trigger: 'blur' },
					  { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
				  ],
				  templateId: [
					  { required: true, message: '请选择表单流程', trigger: 'change' }
				  ]
				}
			}
		},
		methods:{
			init: function(){
				this.loadWorkflowList();
			},
			loadWorkflowList: function(){
				
				var _this = this;
				
				$.http.get({
					url: (appBasePath + '/snail/form.do?method=getCTPTemplateListForCAP4'),
					header: {'Accept': 'application/json'},
					onsuccess: function(result, status, xhr){
						
						if(200 == result.code){
							
							_this.formData.options.workflow = result.data;
							
						}else{
							alert(result.message);
						}
					},
					onerror: function(responseText, status, xhr){
						console.log(responseText);
					}
				});
			},
			getFormData: function(){
				
				var data = {
					name: this.formData.fields.name.trim(),
					workflowId: this.formData.fields.workflowId,
					formId: this.formData.fields.formId,
					templateId: this.formData.fields.templateId,
					description: this.formData.fields.description
				}
				
				return data;
			},
			onWorkflowChang: function(value){
				
				if($.isBlank(value) || !this.formData.options.workflow){
					return;
				}
				
				var workflow = null;
				
				for(var i=0,len=this.formData.options.workflow.length; i<len; i++){
					
					workflow = this.formData.options.workflow[i];
					
					if(value === workflow.id){
						
						this.formData.fields.workflowId = workflow.workflow_id;
						this.formData.fields.formId = workflow.form_appid;
						
						break;
					}					
				}
			},
			onSave: function(){
				
				var _this = this;
				
				return this.$refs.formData.validate().then(validateResult => {
						
					return new Promise(function (resolve, reject) {
						
						try{
							
							var result = $.http.post({
								url: (appBasePath + '/snail/form.do?method=createCipFlowEvent'),
								header: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'},
								data: _this.getFormData()
							});
							
							resolve(result);
							
						}catch(err){
							resolve({code: 500, message: ('创建失败！err:' + err), data: null});
						}
					});
					
				}).catch( error => {
					
					return new Promise(function (resolve, reject) {
						
						resolve({code: 500, message: '数据校验不通过！', data: null});
						
					});
				});				
			}
		},
		created: function(){
			this.init();
		}
	});
	

	win['OK'] = function(){
		
		return app.onSave();
	}
	
})(Snail, window, document);